package cn.aylog.service.impl;

import cn.aylog.dao.MoneyDao;
import cn.aylog.pojo.Money;
import cn.aylog.service.MoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MoneyServiceImpl implements MoneyService {

    @Autowired
    private MoneyDao dao;

    private boolean isNull(Object o) {return o == null;}

    @Override
    public boolean transMoney(Money from, Money to, double money) {
        if(isNull(from) || isNull(to) || money<0) return false;
        from = dao.selectByUserId(from.getUserId());
        to = dao.selectByUserId(to.getUserId());
        if(isNull(from) || isNull(to)) return false;
        if(from.getMoney() < money) return false;
        // 计算金额
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        // 更新金额
        dao.update(from);
        int i = 1/0;
        dao.update(to);
        return true;
    }
}
